package org.medicalinstrument.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

import org.medicalinstrument.bean.*;

import org.medicalinstrument.service.PurchaseOrderService;
import org.medicalinstrument.service.ShoppingCartService;
import org.medicalinstrument.util.DateAndOrderID;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static org.medicalinstrument.util.DateAndOrderID.getfullDate;

/**
 * @ClassName ShoppingCartAction
 * @Description 购物车action类
 * @AUTHOR cirly
 * @DaTe 2018/11/3 20:58
 * @Modify by
 * @Version 1.0
 **/
public class ShoppingCartAction extends ActionSupport {
    private String accountNo;
    private String returnString;
    private String custPhone;
    private String productQR;
    private int buyAmount;
   // private List shoppingList;
    private String list;
    private String qrList;
    private int productNumber;

    public int getProductNumber() {
        return productNumber;
    }

    public void setProductNumber(int productNumber) {
        this.productNumber = productNumber;
    }

    public String getQrList() {
        return qrList;
    }

    public void setQrList(String qrList) {
        this.qrList = qrList;
    }

    public String getList() {
        return list;
    }

    public void setList(String list) {
        this.list = list;
    }




    public int getBuyAmount() {
        return buyAmount;
    }

    public void setBuyAmount(int buyAmount) {
        this.buyAmount = buyAmount;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getProductQR() {
        return productQR;
    }

    public void setProductQR(String productQR) {
        this.productQR = productQR;
    }

    public int getButAmount() {
        return buyAmount;
    }

    public void setButAmount(int butAmount) {
        this.buyAmount = butAmount;
    }


    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getReturnString() {
        return returnString;
    }

    public void setReturnString(String returnString) {
        this.returnString = returnString;
    }


    private ShoppingCartService ss = new ShoppingCartService();
    private PurchaseOrderService ps = new PurchaseOrderService();
    /**
     *@Author cirly
     *@Description 获取用户的购物车信息
     *@Date 20:05 2019/3/18
     *@Modify by
     *@Version 1.0
     */
    public String getShoopingCart() throws SQLException {
        returnString = ss.customerShopping(accountNo);
       // System.out.println("action:  "+returnString);
        if ("shoppingCart_null".equals(returnString)){
            return ERROR;
        }else{
            return SUCCESS;
        }
    }
    /**
     *@Author cirly
     *@Description 商品加入购物车
     *@Date 19:23 2019/3/19
     *@Modify by 
     *@Version 1.0
     */

    public String insertShoppingCart() throws SQLException {
        ShoppingCart cart = new ShoppingCart();
        cart.setAccountNo(accountNo);
        cart.setProductQR(productQR);
        cart.setBuyAmount(buyAmount);
        cart.setCustPhone(custPhone);
      // System.out.println("cart :  "+cart);
        returnString = ss.insertShoppingCart(cart);
        
        return SUCCESS;
    }


    public String buyShopping() throws SQLException {
        List shoppingList = JSON.parseArray(list, BuyInfo.class);

        long l = System.currentTimeMillis();
        String orderTime = DateAndOrderID.getDate(l);
        String time = getfullDate(l);


        List<PurchaseOrderDetail> detailList = new ArrayList<PurchaseOrderDetail>();
    /*



    */
        PurchaseOrderMaster pm = new PurchaseOrderMaster();
        for(int i = 0 ; i < shoppingList.size() ; i ++ ){
            BuyInfo info = new BuyInfo();
            info = (BuyInfo) shoppingList.get(i);
           // System.out.println(info);

            PurchaseOrderDetail pd = new PurchaseOrderDetail();
            pm.setAccountNo(info.getAccountNo());
            pm.setCustPhone(info.getCustPhone());
            pm.setOrderId(info.getOrderID());
            pm.setBuyPrice(info.getBuyPrice());
            pm.setRealPayPrice(info.getBuyPrice());
            pm.setOrderState("1");
            pm.setOrderTime(info.getBuyDate());
            pm.setMessage(info.getMessage());


            pd.setAccountNo(info.getAccountNo());
            pd.setCustPhone(info.getCustPhone());
            pd.setOrderID(info.getOrderID());
            pd.setPrice(info.getBuyPrice());
            pd.setOrderTime(info.getBuyDate());
            pd.setProductQR(info.getProductQR());
            pd.setBuyTime(getfullDate(l));
            pd.setBuyNumber(info.getBuyAmount());
            pd.setPrice(info.getBuyPrice());
            detailList.add(pd);
            System.out.println(pd);
       }
        returnString = ps.buyShoppingCart(pm,detailList);
        System.out.println(returnString);
        if (returnString.equals("insert_success")){
            return SUCCESS;
        }else{
            return ERROR;
        }

    }

    /**
     *@Author cirly
     *@Description 判断要加入的商品是否已存在于购物车
     *@Date 13:16 2019/3/24
     *@Modify by
     *@Version 1.0
     */

    public String isExist() throws SQLException {
        returnString = ss.isExist(productQR);
        if(returnString.equals("exist")){
            return "error";
        }else{
            return "success";
        }
    }
    /**
     *@Author cirly
     *@Description 页面删除购物车内容
     *@Date 21:48 2019/3/24
     *@Modify by
     *@Version 1.0
     */
    public String deleteCart() throws SQLException {
        List qrList = JSON.parseArray(getQrList());
        System.out.println(qrList);
        ShoppingCart cart = new ShoppingCart();
        returnString = ss.delete(qrList);
        if(returnString.equals("delete_success")){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

    /**
     *@Author cirly
     *@Description 点击数量后触发，修改购物车数量
     *@Date 15:16 2019/3/26
     *@Modify by
     *@Version 1.0
     */
    public String updateShopping() throws SQLException{
        ShoppingCart cart = new ShoppingCart();
        cart.setProductQR(productQR);
        cart.setBuyAmount(buyAmount);
        returnString = ss.updateShopping(cart);
        if(returnString.equals("delete_success")){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }

}